//
//  LBCategorySelectedChildView.m
//  jslj
//
//  Created by luckyRoy on 2017/10/10.
//  Copyright © 2017年 广东联结电子商务有限公司. All rights reserved.
//

#import "LBCategorySelectedChildView.h"
#import <Masonry.h>
#import <MJExtension.h>
#import <UIColor+LBColor.h>
#import <NSString+LBString.h>

static CGFloat const LBCategorySelectedViewTitleSpace = 5;

static CGFloat const LBCategorySelectedViewImageHeight = 15.0;

static CGFloat const LBCategorySelectedChildViewHeight = 50.0;

static CGFloat const LBCategorySelectedChildViewTitleFontSize = 14.0;

@interface LBCategorySelectedChildView ()

@property (nonatomic, strong) UIView *bgView;

@property (nonatomic, strong) UILabel *titleLabel;

@property (nonatomic, strong) UIImageView *delImageView;

@property (nonatomic, copy) NSString *title;

@property (nonatomic, copy) LBCategorySelectedChildViewClick viewClick;

@end

@implementation LBCategorySelectedChildView

- (instancetype)initWithTitle:(NSString *)title
                     complete:(LBCategorySelectedChildViewClick)complete
{
    self = [super initWithFrame:self.bounds];
    if (self) {
        self.viewClick = complete;
        self.title = title;
        [self createAllViewWithTitle:title];
    }
    return self;
}

- (void)createAllViewWithTitle:(NSString *)title
{
    [self addSubview:self.bgView];
    [self addSubview:self.delImageView];
    
    CGFloat space = 3;
    
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    [self.delImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(space);
        make.top.equalTo(self).offset(-space);
        make.size.mas_equalTo(CGSizeMake(LBCategorySelectedViewImageHeight, LBCategorySelectedViewImageHeight));
    }];
    
    //    UITapGestureRecognizer *tagGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickAction:)];
    //    [self addGestureRecognizer:tagGesture];
    
    [self.titleLabel setText:title];
}

- (void)refreshUIByIsEdiotr:(BOOL)isEdiotr
                   newTitle:(NSString *)title
{
    if (isEdiotr) {
        [self.delImageView setHidden:NO];
    } else {
        [self.delImageView setHidden:YES];
    }
    [self.titleLabel setText:title];
}

#pragma mark - click

#pragma mark - getter

- (UIView *)bgView
{
    if (!_bgView) {
        _bgView = [[UIView alloc]init];
        _bgView.backgroundColor = [UIColor colorWithRed:244/255.0 green:250/255.0 blue:255/255.0 alpha:1];
        
        [_bgView addSubview:self.titleLabel];
        
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(_bgView);
        }];
    }
    return _bgView;
}

- (UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.textColor = [UIColor blue];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.font = [UIFont systemFontOfSize:LBCategorySelectedChildViewTitleFontSize];
        _titleLabel.numberOfLines = 0;
    }
    return _titleLabel;
}

- (UIImageView *)delImageView
{
    if (!_delImageView) {
        _delImageView = [[UIImageView alloc]init];
        [_delImageView setImage:[UIImage imageNamed:@"Category.bundle/lb_category_close"]];
    }
    return _delImageView;
}

+ (CGSize)sizeForViewByModel:(LBCategoryModel *)model
{
    NSString *showTitle = model.name;
    CGFloat width = [NSString getTextSizeWithContent:showTitle Font:[UIFont systemFontOfSize:LBCategorySelectedChildViewTitleFontSize] Width:[[UIScreen mainScreen] bounds].size.width].width;
    //lable space to bgView bgViewSpace to self
    width += LBCategorySelectedViewTitleSpace * 4 + 20;
    
    CGSize theSize = CGSizeMake(width, LBCategorySelectedChildViewHeight);
    
    return theSize;
}

@end

